import { PRIORITY_VIEW } from "../../../Core/CoreDefine";
import { GCtrl } from "../../../Core/GCtrl";
import { GListView } from "../../../Core/GView/GListView";
import GPage from "../../../Core/GView/GPage";
import { CMsg, LISTENTTYPE } from "../../Common/Define";
import { Res } from "../../Common/UIResources";
import GameMgr from "../../Logic/GameMgr";
import { IHeroSort, SHeroSort } from './../../Logic/HeroMgr';
import HeroCardItem from "./HeroCardItem";

const { ccclass, property, menu, executeInEditMode } = cc._decorator;
@ccclass @menu('View/Hero/HeroCtrl')

export default class HeroCtrl extends  GPage {
    @property(cc.ScrollView) cardView: cc.ScrollView = null;
    @property(cc.Node) content: cc.Node = null;
    @property(cc.Node) mask: cc.Node = null;


    @property({type: cc.Node,tooltip: "武将模块"})  tog1Node: cc.Node = null;

    @property({type: cc.Node,tooltip: "图鉴模块"})  tog2Node: cc.Node = null;

    @property({type: cc.Node,tooltip: "编队模块"})  tog3Node: cc.Node = null;

    @property({type: cc.Node,tooltip: "品质多选"})  qualityTog: cc.Node = null;

    private _listView: GListView = null;
    private _curListIndex: number = 0;

    private _curPageIndex: number = 0;
    onPageIn() {
        this.initEmbattle();
    }

    onGLoad() {

        /**监听获取碎片道具刷新 */
        GCtrl.ES.on(CMsg.client.bag.onBagItemChange, this, this.initEmbattle.bind(this), PRIORITY_VIEW);
        GCtrl.ES.on(CMsg.client.equip.AddSynthesisEquip, this, this.initEmbattle.bind(this), PRIORITY_VIEW);
        GCtrl.ES.on(CMsg.client.hero.onHeroLvUp, this, this.initEmbattle.bind(this), PRIORITY_VIEW);
        GCtrl.ES.on(CMsg.client.hero.onHeroBreach, this, this.initEmbattle.bind(this), PRIORITY_VIEW);

        /**监听添加英雄或者解锁英雄后刷新 */
        GCtrl.ES.on(CMsg.client.hero.onAddHero, this, this.initEmbattle.bind(this), PRIORITY_VIEW);

        this.refresh();
    }

    /**初始化View */
    initEmbattle() {
        if (!this._listView) {
            this.assetImpl.prefab(Res.prefab.item.heroCardItem, (prefab: cc.Prefab) => {
                this._listView = new GListView({
                    scrollview: this.cardView,
                    mask: this.mask,
                    content: this.content,
                    itemTpl: cc.instantiate(prefab),
                    itemSetter: this.onItemSetter.bind(this),
                    column: 3,
                    gapY: 20,
                    gapX: 30,
                    padingY: 20,
                    padingX: 15,
                    isCbClass: true,
                    isWidget: true,
                });
                this.dealWithData()
            })
        } else {
            this.dealWithData()
        }
    }

    refresh(){
        this.tog1Node.active = this._curPageIndex==0;

        this.tog2Node.active = this._curPageIndex==1;

        this.tog3Node.active = this._curPageIndex==2;

        this.qualityTog.active = this._curPageIndex!=2;
    }

    protected dealWithData() {
        let allHero = GameMgr.rHeroData.getHaveHeros(this._curListIndex);
        let hava = allHero.have;
        let notHave = allHero.notHave;
        let havaSorts = [IHeroSort.sortLvUp, IHeroSort.sortStart, IHeroSort.sortZhanli];
        let haveSort = (a: IHeroData, b: IHeroData): number => {
            return IHeroSort.sorts(havaSorts, a, b)
        }
        hava.sort(haveSort);
        let notHaveSorts = [SHeroSort.sortChip, SHeroSort.sortTrait, SHeroSort.sortStart, SHeroSort.sortZhanli];
        let notHaveSort = (a: SHeroDataRaw, b: SHeroDataRaw): number => {
            return SHeroSort.sorts(notHaveSorts, a, b);
        }
        notHave.sort(notHaveSort);
        let arr = [...hava, ...notHave];
        this._listView.setData(arr)
    }

    /**武将品质切换*/
    protected btnOnce(CustomEventData, index) {
        this._curListIndex = Number(CustomEventData.node._name);
        GCtrl.ES.emit(CMsg.client.home.onBattleItem, -1);
        this.dealWithData();
    }

    /**0 武将，1图鉴，2编队 */
    protected btnPage(CustomEventData, index) {
        this._curPageIndex = Number(CustomEventData.node._name);
        //GCtrl.ES.emit(CMsg.client.home.onBattleItem, -1);
        // this.dealWithData();

        this.refresh();
    }

    /**初始每个卡牌 */
    public onItemSetter(item: HeroCardItem, data: SHeroDataRaw | IHeroData, index: number) {
        item.init(data, index, LISTENTTYPE.listent2);
    }
}